package com.mingtu.guards.activity;

import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.utils.MyConstant;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;

import butterknife.BindView;

public class MsgDetailsActivity extends MyBaseActivity {


    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.srl_up)
    SmartRefreshLayout srlUp;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_details;
    }

    @Override
    protected void initView() {
        setModuleTitle("通知详情");
        setTitleBarBackground(getResources().getDrawable(R.drawable.titlebar_line_bottom));

    }

    @Override
    protected void initData() {
        initPullRefresh();
    }

    @Override
    protected void getData() {
        String content = getIntent().getStringExtra("detail");
        String title = getIntent().getStringExtra("title");
        String time = getIntent().getStringExtra("time");
        String id = getIntent().getStringExtra("id");

        WebSettings webSettings = webView.getSettings();
        webSettings.setDisplayZoomControls(false); //隐藏webview缩放按钮
        webSettings.setJavaScriptEnabled(true);//支持js
//        webSettings.setBuiltInZoomControls(true); // 显示放大缩小
//        webSettings.setSupportZoom(true); // 可以缩放
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setTextZoom(230);
        webSettings.setStandardFontFamily("family");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 得到 URL 可以传给应用中的某个 WebView 页面加载显示
                return true;
            }
        });


        if (!StringUtils.isEmpty(time)) {
            tvTime.setText("时间：" + time);
        }


        if (!StringUtils.isEmpty(title)) {
            tvTitle.setText(title + "");
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.post(() -> {
                if (tvTitle.getLineCount() > 1)
                    //多余一行左对齐
                    tvTitle.setGravity(Gravity.LEFT);
                else
                    //一行中间对齐
                    tvTitle.setGravity(Gravity.CENTER_HORIZONTAL);
            });
        }

        if (!StringUtils.isEmpty(content)) {
            webView.loadDataWithBaseURL(null, new HtmlFormat().getNewContent(content), "text/html", "UTF-8", null);
        }
        getMsgDetails(id);

    }

    private void initPullRefresh() {
        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        srlUp.setEnableLoadMore(false);
        srlUp.setEnableOverScrollDrag(true);
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);
            }
        });
    }


    public class HtmlFormat {
        public String getNewContent(String htmltext) {
            Document doc = Jsoup.parse(htmltext);
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width", "100%").attr("height", "auto");
            }
            return doc.toString();
        }
    }

    /**
     * 获取新闻列表
     *
     * @param id
     */
    private void getMsgDetails(String id) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", id);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_MY_MESSAGE_DETAILS + "/" + id)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            //此处不用做任何操作，为了标记消息为已读状态

                        }
                    }
                });
    }
}